
<?php
require_once LIBRARIES_ENTITIES_DATABASE;
require_once LIBRARIES_ENTITIES_CATEGORIES;
require_once LIBRARIES_ENTITIES_LOGO;
require_once LIBRARIES_ENTITIES_USERS;
require_once LIBRARIES_ENTITIES_POLL_CAU_HOI;


require_once LIBRARIES_TOOLS_UTILITY;
require_once LIBRARIES_TOOLS_XTEMPLATE;
require_once LIBRARIES_TOOLS_PAGERBACKEND;
    class cauhoiController
    {

        public function invoke()
        {
        	 // function list
        $groupFunctions = $_SESSION['groupFunctions'];

        // directory path
        $dir = dirname(__FILE__);

        $view = '';
        if (isset($_GET['view'])) {
            $view = $_GET['view'];
        }

        $action = '';
        if (isset($_POST['hdAction'])) {
            $action = $_POST['hdAction'];
        }



        $task = '';
        if (isset($_GET['task'])) {
            $task = $_GET['task'];
        }


        $rowToView = 10;
        if (isset($_POST["limit"])) {
            $rowToView = $_POST["limit"];
        }


        switch ($action) {
             case "publish":
                    $values = $_POST["cid"];
                    $result = $this->updateStatus($values, true);
                    break;
                case "unpublish":
                    $values = $_POST["cid"];
                    $result = $this->updateStatus($values, false);
                    break;
             case 'orderdown':
                    $ID = $_POST["hdID"];
                    $result = $this->orderVideo($ID, 'down');

                    Utility::redirectCurrentPage();
                    break;
                case 'orderup':
                    $ID = $_POST["hdID"];
                    $result = $this->orderVideo($ID, 'up');
                    Utility::redirectCurrentPage();
                    break;
            case 'remove':
                $values = $_POST["cid"];
                $result = $this->deleteQuestions($values);
                if ($result == OK) {
                    Utility::messageBox('Xóa thành công');
                }else {
                	Utility::messageBox('Dữ liệu này đang được sử dụng trong đánh giá');
                }
                break;
            case 'add':
            case 'addAndExit':

                    $noidung = $_POST['txtnoidung'];
                   

                    $result = $this->addQuestions($noidung);

                    if ($result == OK) {
                        if ($action == 'add') {
                            Utility::messageBox('Thêm thành công');
                        } else {
                            Utility::redirect(".?com=com_cauhoi&task=view");
                        }
                    }
               
                break;
            case 'edit':
            case 'editAndExit':

                $ID = $_POST['hdID'];
                $noidung = $_POST['txtnoidung'];

             
                $result = $this->updateQuestions($ID, $noidung);

                if ($result == OK) {
                    if ($action == 'edit') {
                        Utility::messageBox('Sửa thành công');
                    } else {
                        Utility::redirect(".?com=com_cauhoi&task=view");
                    }
                }
                break;
            case 'cancel':
                Utility::redirect(".?com=com_cauhoi&task=view");
                break;
        }






        if ($task == 'view') {

            // check view permission
            if (Utility::checkPermission('view', $groupFunctions, QUESTIONS_MANAGEMENT)) {

                $viewFile = $dir . '/views/list.php';
                $xtemplate = new XTemplate($viewFile);

                // CHECK PERMISSION
                // check add button
                if (Utility::checkPermission('add', $groupFunctions, QUESTIONS_MANAGEMENT)) {
                    $xtemplate->parse('main.add_button');
                }
                // check delete button
                if (Utility::checkPermission('delete', $groupFunctions, QUESTIONS_MANAGEMENT)) {
                    $xtemplate->parse('main.delete_button');
                }
                // check enable button
                if (Utility::checkPermission('enable', $groupFunctions, QUESTIONS_MANAGEMENT)) {
                    $xtemplate->parse('main.enable_button');
                }
                // check disable button
                if (Utility::checkPermission('disable', $groupFunctions, QUESTIONS_MANAGEMENT)) {
                    $xtemplate->parse('main.disable_button');
                }


                $indexCheckBox = 1;

                $rurrentPage = 1;
                if (isset($_POST["page"])) {
                    $rurrentPage = $_POST["page"];
                }


                // chỉ mục (số thứ tự của bài viết) bài viết
                $index = 1;

                $Questions = $this->getQuestionsList();

                $pager = PagerBackEnd::getPagerData(count($Questions), $rowToView, $rurrentPage);


                if ($Questions != null) {
                    $indexCheckBox=1;
                    foreach ($Questions as $question) {

                        if ($index > $pager->offset && $index <= $pager->limit) {

                          

                                $xtemplate->assign('INDEX', $index);
                                 $xtemplate->assign('INDEX_CHECKBOX', $indexCheckBox);
                                $xtemplate->assign('ID', $question->getid());
                                $xtemplate->assign('noidung', $question->getnoidung());
                               
                                 // check update permission                            
	                            if (Utility::checkPermission('update', $groupFunctions, QUESTIONS_MANAGEMENT)) {
	                                $xtemplate->parse('main.cauhoi.update_button');
	                            } else {
	                                $xtemplate->parse('main.cauhoi.update_button_disable');
	                            }

                                // status
                                if ($question->getStatus() == 1) {
                                    $xtemplate->assign('STATUS_ACTION', 'unpublish');
                                    $xtemplate->assign('STATUS_IMAGE', 'publish_g.png');
                                } else {
                                    $xtemplate->assign('STATUS_ACTION', 'publish');
                                    $xtemplate->assign('STATUS_IMAGE', 'publish_r.png');
                                }


                             	$xtemplate->parse('main.cauhoi');	
                        }
                        $index++;
                        $indexCheckBox++;
                    }
                }

                $xtemplate->assign('LIMIT_LIST', Utility::getLimitList($rowToView));
                $xtemplate->assign('ROW_TO_VIEW', $rowToView);
                $xtemplate->assign('CURRENT_PAGE', $rurrentPage);
                $xtemplate->assign('AMOUNT_OF_PAGE', $pager->numPages);
                $xtemplate->assign('PAGING_HTML', $pager->paging);
                $xtemplate->parse('main');
                $xtemplate->out('main');
            } else {
                echo ERROR_PERMISSION;
            }
        } else if ($task == 'add') {

            // check add permission
            if (Utility::checkPermission('add', $groupFunctions, QUESTIONS_MANAGEMENT)) {
                $xtemplate = new XTemplate($dir . '/views/add.php');

                $xtemplate->parse('main');
                $xtemplate->out('main');
            } else {
                echo ERROR_PERMISSION;
            }
        } else if ($task == 'edit') {

            // check add permission
            if (Utility::checkPermission('update', $groupFunctions, QUESTIONS_MANAGEMENT)) {
                $ID = '';
                if (isset($_GET['ID'])) {
                    $ID = $_GET['ID'];
                }

                $Questions = $this->getQuestionsByID($ID);

                if ($Questions != null) {

                    $viewFile = $dir . '/views/edit.php';
                    $xtemplate = new XTemplate($viewFile);
                    $xtemplate->assign('ID', $Questions->getid());
                    $xtemplate->assign('noidung', $Questions->getnoidung());
                   

                    $xtemplate->parse('main');
                    $xtemplate->out('main');
                } else {
                    Utility::redirect('.?com=com_cauhoi&task=view');
                }
            } else {
                echo ERROR_PERMISSION;
            }
        }
    }














    private function getQuestionsList() {
        $db = new Database();
        try {
            $questions = new Poll_cauhoi();
            $result = $questions->getList_($db);
            $db->submitChanges();
            return $result;
        } catch (Exception $exc) {
            echo $exc->getMessage();
            $db->rollback();
            exit();
        }
    }

  


    private function addQuestions($noidung) {
        $db = new Database();
        try {
            $question = new Poll_cauhoi();
            $question->setnoidung($noidung);
            $question->setStatus(0);
            $question = $question->insert($db);
            $db->submitChanges();

            return OK;
        } catch (PDOException $ex) {
            echo $ex->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function deleteQuestions($values) {
        $db = new Database();
        try {
            for ($i = 0; $i < count($values); $i++) {
                 $question = new Poll_cauhoi();
                $question->setid($values[$i]);
                $question->delete($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $ex) {
            Utility::messageBox('Dữ liệu này đang được sử dụng trong đánh giá');
            Utility::redirect(".?com=com_cauhoi&task=view");
            $db->rollback();
            exit();
        }
    }

    private function updateQuestions($ID, $noidung) {
        $db = new Database();
        try {
             $question = new Poll_cauhoi();
              $question->setnoidung($noidung);
            $question->setid($ID);
            $question->update($db);

            $db->submitChanges();

            return OK;
        } catch (PDOException $ex) {
            echo $ex->getMessage();
            $db->rollback();
            exit();
        }
    }

    private function getQuestionsByID($ID) {
        $db = new Database();
        try {
             $question = new Poll_cauhoi();
            $question->setid($ID);
            $result = $question->getByID($db);
            $db->submitChanges();
            return $result;
        } catch (PDOException $ex) {

            echo $ex->getMessage();
            $db->rollback();
            exit();
        }
    }

    

   

 



 // hàm cập nhật trạng thái bài viết
    private function updateStatus($values, $status) {

        try {
            $db = new Database();
            for ($i = 0; $i < count($values); $i++) {
                 $question = new Poll_cauhoi();
                $question->setid($values[$i]);
                if ($status == true) {
                    $question->setStatus(1);
                } else {
                    $question->setStatus(0);
                }
                $question->updateStatus($db);
                $question->updateStatus_All($db);
            }
            $db->submitChanges();
            return OK;
        } catch (PDOException $exc) {
            echo $exc->getMessage();
            exit();
        }
    }








            
        

    }
?>

